package com.hzcx.app.simplechat.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PhoneCodeEditText extends RelativeLayout {

    @BindView(R.id.tv_num_one)
    TextView tvNumOne;
    @BindView(R.id.tv_num_two)
    TextView tvNumTwo;
    @BindView(R.id.tv_num_three)
    TextView tvNumThree;
    @BindView(R.id.tv_num_four)
    TextView tvNumFour;
    @BindView(R.id.et_code)
    EditText etCode;
    // 根布局
    private View view;
    private Context context;
    private List<String> codeList;

    public PhoneCodeEditText(Context context) {
        super(context);
        initView(context);
    }

    public PhoneCodeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public PhoneCodeEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.view_phone_code, this);
        ButterKnife.bind(this);
        codeList=new ArrayList<>();
        initEvent();
    }

    /**
     *  监听edit输入
     */
    private void initEvent(){
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(EmptyUtils.isNotEmpty(editable)&&editable.length()==1){
                    etCode.setText("");
                    if(codeList.size()<4){
                        codeList.add(editable.toString());
                        setCode();
                        if(codeList.size()==4&&EmptyUtils.isNotEmpty(onPhoneCodeListener)){
                            onPhoneCodeListener.complte(getPhoneCode());
                        }
                    }
                }else if(EmptyUtils.isNotEmpty(editable)&&editable.length()==4){
                    for (int i=0;i<editable.length();i++){
                        etCode.setText("");
                        if(codeList.size()<4){
                            codeList.add(String.valueOf(editable.charAt(i)));
                            setCode();
                            if(codeList.size()==4&&EmptyUtils.isNotEmpty(onPhoneCodeListener)){
                                onPhoneCodeListener.complte(getPhoneCode());
                            }
                        }
                    }
                }
            }
        });

        // 监听验证码删除按键
        etCode.setOnKeyListener((view, keyCode, keyEvent) -> {
            if (keyCode == KeyEvent.KEYCODE_DEL && keyEvent.getAction() == KeyEvent.ACTION_DOWN && codeList.size()>0) {
                codeList.remove(codeList.size()-1);
                setCode();
                return true;
            }
            return false;
        });
    }

    /**
     *  设置验证码
     */
    private void setCode(){
        String code1 = "";
        String code2 = "";
        String code3 = "";
        String code4 = "";
        if(codeList.size()>=1){
            code1 = codeList.get(0);
        }
        if(codeList.size()>=2){
            code2 = codeList.get(1);
        }
        if(codeList.size()>=3){
            code3 = codeList.get(2);
        }
        if(codeList.size()>=4){
            code4 = codeList.get(3);
        }
        tvNumOne.setText(code1);
        tvNumTwo.setText(code2);
        tvNumThree.setText(code3);
        tvNumFour.setText(code4);

        setCodeBg();
    }

    /**
     *  设置高亮背景
     */
    private void setCodeBg(){
//        tvNumOne.setBackgroundResource(R.mipmap.ic_view_phone_code_normal_bg);
//        tvNumTwo.setBackgroundResource(R.mipmap.ic_view_phone_code_normal_bg);
//        tvNumThree.setBackgroundResource(R.mipmap.ic_view_phone_code_normal_bg);
//        tvNumFour.setBackgroundResource(R.mipmap.ic_view_phone_code_normal_bg);
//        if(codeList.size()==0){
//            tvNumOne.setBackgroundResource(R.mipmap.ic_view_phone_code_selete_bg);
//        }
//        if(codeList.size()==1){
//            tvNumTwo.setBackgroundResource(R.mipmap.ic_view_phone_code_selete_bg);
//        }
//        if(codeList.size()==2){
//            tvNumThree.setBackgroundResource(R.mipmap.ic_view_phone_code_selete_bg);
//        }
//        if(codeList.size()>=3){
//            tvNumFour.setBackgroundResource(R.mipmap.ic_view_phone_code_selete_bg);
//        }
    }

    /**
     * 获得手机号验证码
     * @return 验证码
     */
    public String getPhoneCode(){
        StringBuilder sb = new StringBuilder();
        for (String code : codeList) {
            sb.append(code);
        }
        return sb.toString();
    }

    private OnPhoneCodeListener onPhoneCodeListener;
    public interface OnPhoneCodeListener{
        void complte(String code);
    }

    /**
     *  设置验证码输入完毕监听
     * @param onPhoneCodeListener
     */
    public void setOnPhoneCodeListener(OnPhoneCodeListener onPhoneCodeListener){
        this.onPhoneCodeListener=onPhoneCodeListener;
    }

}
